package cn.com.bluemoon.daps.standard.controller.api;


import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiStandard;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.standard.api.RemoteStandardDataTypeService;
import cn.com.bluemoon.daps.standard.entity.DapStandardDataType;
import cn.com.bluemoon.daps.standard.service.DapStandardDataTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 数据库字段基础类型数据表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-08-07
 */
@RestController
@Slf4j
@RequestMapping(DapsApiStandard.API_SYS_STD_DATA_TYPE)
@Api(tags = {"FeignClient服务-数据类型管理Api"})
@BmPermission(value = "", name = "查询数据类型基础数据", ignore = true)
public class DapStandardDataTypeApi implements RemoteStandardDataTypeService {

    @Autowired
    DapStandardDataTypeService dataTypeService;

    @GetMapping("/dataType")
    @ApiOperation(value = "获取数据库对应的字段类型")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapStandardDataType.class)})
    public ResultBean<List<DapStandardDataType>> getDataTypeByDbType(@RequestParam("dbType") @ApiParam("数据库类型，1 mysql, 2 pgsql, 3 hive")
                                                                         Integer dbType) {
        DatabaseType databaseType = DatabaseType.MYSQL;
        if (2 == dbType) {
            databaseType = DatabaseType.PGSQL;
        } else if (3 == dbType) {
            databaseType = DatabaseType.HIVE;
        }
        LambdaQueryWrapper<DapStandardDataType> eq = dataTypeService.bmLambdaQuery().eq(DapStandardDataType::getDbType, databaseType);
        return ResultBean.ok(dataTypeService.list(eq));
    }

    /**
     * 获取数据库字段类型基础数据
     *
     * @return 获取数据库字段类型基础数据
     */
    @GetMapping("/dataTypes")
    @ApiOperation(value = "获取数据库对应的字段类型")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapStandardDataType.class)})
    public ResultBean<List<DapStandardDataType>> listDataTypes(){
        return ResultBean.ok(dataTypeService.list(dataTypeService.bmLambdaQuery()));
    }
}
